/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class ChildbearingPageElementTests extends BasePageElementTests {
	private ChildbearingPageElement	element	= new CleanRoomChildbearingPageElement("myDiv");

	@Test
	public void testAddChild() {
		final CleanRoomChildlessPageElement element2 = new CleanRoomChildlessPageElement("myImage");
		this.element.addPageElement(element2);
		assertNull(element2.getRequest());
		final PageElement findElement = this.element.findElement("myImage");
		assertEquals(element2, findElement);
		this.element.removePageElement("myImage");
		assertFalse(this.element.hasChildren());

		this.prepPageElement(this.element);
		this.element.addPageElement(element2);
		this.element.load();
		final HttpServletRequest request = findElement.getRequest();
		assertNotNull(request);
	}

	@Test
	public void checkRender() throws IOException {
		MockHttpServletResponse r = this.prepPageElement(this.element);
		this.element.render(new MockHttpServletRequest(), r);

		String contentAsString = r.getContentAsString();
		assertEquals("<div>hello, world</div>", contentAsString);

		this.element = new CleanRoomChildbearingPageElement("myDiv");
		r = this.prepPageElement(this.element);
		this.element.setAttribute("title", "a div");
		this.element.render(new MockHttpServletRequest(), r);

		contentAsString = r.getContentAsString();
		assertEquals("<div title=\"a div\">hello, world</div>", contentAsString);
	}

	@Test
	public void checkingNulls() {
		this.element.addPageElement(null);
		assertFalse(this.element.hasChildren());

		assertNull(this.element.findElement(null));
		assertNull(this.element.findElement("boo"));

		this.element.removePageElement("boo");
		assertFalse(this.element.hasChildren());
	}

	private static class CleanRoomChildbearingPageElement extends ChildbearingPageElement {

		public CleanRoomChildbearingPageElement(final String _controlId) {
			super(_controlId);
			this.setTag("div");
			this.setSupportsAttributes(true);

			this.addBeforeRenderCallBack(new IPageElementCallback<CleanRoomChildbearingPageElement>() {

				@Override
				public void execute(final CleanRoomChildbearingPageElement _elementBeingLoaded) {
					_elementBeingLoaded.addPageElement(new Content("hello, world"));
				}
			});
		}

	}

	private static class CleanRoomChildlessPageElement extends ChildlessPageElement {
		{
			this.setTag("img");
		}

		public CleanRoomChildlessPageElement(final String _controlId) {
			super(_controlId);
		}
	}
}
